import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.0
import REngine 1.0
import "Common.js" as COM

RPage {
    id: settingView
    property real rowHeight: 100
    property color backgroundColor: "white"
    property color fontColor: "black"
    property string currentFile: ""

    function reload() {
        fontSize.value = engine.setting(REngine.TextFontPixelSize)
        fontFamily.value = engine.setting(REngine.TextFontFamiliy)
        lineSpace.value = engine.setting(REngine.TextLineSpace)
        wordSpace.value = engine.setting(REngine.TextFontWordSpacing)
        letterSpace.value = engine.setting(REngine.TextFontLetterSpacing)
        bold.value = engine.setting(REngine.TextFontBold)
        orientation.value = orientationString(engine.setting(REngine.Orientation))
        screen.allowedOrientations = engine.setting(REngine.Orientation)
        showToolBar.value = engine.setting(REngine.ShowToolbar)
        textColor.value = engine.setting(REngine.TextColor)
        backgroundColor.value = engine.setting(REngine.TextBackgroud)
        defaultCodec.value = engine.setting(REngine.Codec) === "Auto" ? qsTr("Auto") : engine.setting(REngine.Codec)
        if (currentFile.length > 0) {
            fileCodec.value = engine.fileCurrentCodec(currentFile)
        }
        hideEmptyLine.value = engine.setting(REngine.TextIgnoreEmptyLine)
        showQuickSetting.value = engine.setting(REngine.ShowQuickSetting)
        showToolBar.enabled = showQuickSetting.value
        autoPage.value = engine.setting(REngine.AutoPageTurning)
        autoPageTimeOut.value = engine.setting(REngine.AutoPageTurningTimeOut)
        showAutoPageTime.value = engine.setting(REngine.ShowAutoPageTurningTime)
        disableDisplayBlank.value = engine.setting(REngine.DisableDisplayBlank)
    }

    function orientationString(orientationValue) {
        console.log("orientationValue:", orientationValue)
        switch(orientationValue) {
        case Screen.All:
            return qsTr("Auto")

        case Screen.Portrait:
            return qsTr("Portrait")

        case Screen.Landscape:
            return qsTr("Landscape")

        case Screen.PortraitInverted:
            return qsTr("Portrait Inverted")

        case Screen.LandscapeInverted:
            return qsTr("Landscape Inverted")

        default:
            return qsTr("Unkown")
        }
    }

    Rectangle {
        id: backgroundRect
        anchors.fill:parent
        color: settingView.backgroundColor
        InfoBanner {
            id: infoBanner
        }

        Rectangle {
            id: header
            anchors.top: parent.top
            width: parent.width
            height: 80
            color: "dodgerblue"
            Label {
                anchors.centerIn: parent
                font.bold: true
                font.pixelSize: 35
                color: "White"
                text: qsTr("Setting")
            }
        }

        Flickable {
            clip: true
            anchors.top: header.bottom
            anchors.bottom: parent.bottom
            width: parent.width
            contentWidth: parent.width
            contentHeight: column.height
            Column {
                id: column
                width: parent.width
                SettingViewTextRow {
                    id: fontSize
                    textColor: settingView.fontColor
                    width: parent.width
                    height: settingView.rowHeight
                    name: qsTr("Font Size")
                    value: engine.setting(REngine.TextFontPixelSize)
                    onClicked: {
                        valueSelectionDialog.titleStr = fontSize.name
                        valueSelectionDialog.minimumValue = 20
                        valueSelectionDialog.maximumValue = 70
                        valueSelectionDialog.value = fontSize.value
                        valueSelectionDialog.selectItem = fontSize
                        valueSelectionDialog.open()
                    }
                }
                SettingViewTextRow {
                    id: fontFamily
                    textColor: settingView.fontColor
                    width: parent.width
                    height: settingView.rowHeight
                    name: qsTr("Font Family")
                    valueFontFamily: value
                    value: engine.setting(REngine.TextFontFamiliy)
                    onClicked: {
                        fontSelectionDialog.open()
                    }
                }
                SettingViewTextRow {
                    id: lineSpace
                    textColor: settingView.fontColor
                    width: parent.width
                    height: settingView.rowHeight
                    name: qsTr("Line Space")
                    value: engine.setting(REngine.TextLineSpace)
                    onClicked: {
                        valueSelectionDialog.titleStr = lineSpace.name
                        valueSelectionDialog.minimumValue = 0
                        valueSelectionDialog.maximumValue = 20
                        valueSelectionDialog.value = lineSpace.value
                        valueSelectionDialog.selectItem = lineSpace
                        valueSelectionDialog.open()
                    }
                }
                SettingViewTextRow {
                    id: wordSpace
                    textColor: settingView.fontColor
                    width: parent.width
                    height: settingView.rowHeight
                    name: qsTr("Word Space")
                    value: engine.setting(REngine.TextFontWordSpacing)
                    onClicked: {
                        valueSelectionDialog.titleStr = wordSpace.name
                        valueSelectionDialog.minimumValue = 0
                        valueSelectionDialog.maximumValue = 20
                        valueSelectionDialog.value = wordSpace.value
                        valueSelectionDialog.selectItem = wordSpace
                        valueSelectionDialog.open()
                    }
                }
                SettingViewTextRow {
                    id: letterSpace
                    textColor: settingView.fontColor
                    width: parent.width
                    height: settingView.rowHeight
                    name: qsTr("Letter Space")
                    value: engine.setting(REngine.TextFontLetterSpacing)
                    onClicked: {
                        valueSelectionDialog.titleStr = letterSpace.name
                        valueSelectionDialog.minimumValue = 0
                        valueSelectionDialog.maximumValue = 20
                        valueSelectionDialog.value = letterSpace.value
                        valueSelectionDialog.selectItem = letterSpace
                        valueSelectionDialog.open()
                    }
                }
                SettingViewColorRow {
                    id: backgroundColor
                    textColor: settingView.fontColor
                    width: parent.width
                    height: settingView.rowHeight
                    name: qsTr("Background Color")
                    value: engine.setting(REngine.TextBackgroud)
                    onClicked: {
                        colorSelectionDialog.selectItem = backgroundColor
                        colorSelectionDialog.open()
                    }
                }
                SettingViewColorRow {
                    id: textColor
                    textColor: settingView.fontColor
                    width: parent.width
                    height: settingView.rowHeight
                    name: qsTr("Text Color")
                    value: engine.setting(REngine.TextColor)
                    onClicked: {
                        colorSelectionDialog.selectItem = textColor
                        colorSelectionDialog.open()
                    }
                }
                SettingViewBoolRow {
                    id: bold
                    textColor: settingView.fontColor
                    width: parent.width
                    height: settingView.rowHeight
                    name: qsTr("Bold")
                    value: engine.setting(REngine.TextFontBold)
                    onValueChanged: {
                        engine.setSetting(REngine.TextFontBold, value)
                    }
                }

                SettingViewBoolRow {
                    id: disableDisplayBlank
                    textColor: settingView.fontColor
                    width: parent.width
                    height: settingView.rowHeight
                    name: qsTr("Display Always On")
                    value: engine.setting(REngine.DisableDisplayBlank)
                    onValueChanged: {
                        engine.setSetting(REngine.DisableDisplayBlank, value)
                    }
                }

                SettingViewBoolRow {
                    id: nightMode
                    textColor: settingView.fontColor
                    width: parent.width
                    height: settingView.rowHeight
                    name: qsTr("Night Mode")
                    value: engine.setting(REngine.NightMode)
                    onValueChanged: {
                        engine.setSetting(REngine.NightMode, value)
                    }
                }


                SettingViewBoolRow {
                    id: autoPage
                    textColor: settingView.fontColor
                    width: parent.width
                    height: settingView.rowHeight
                    name: qsTr("Auto Page Turning")
                    value: engine.setting(REngine.AutoPageTurning)
                    onValueChanged: {
                        engine.setSetting(REngine.AutoPageTurning, value)
                    }
                }

                SettingViewTextRow {
                    id: autoPageTimeOut
                    textColor: settingView.fontColor
                    width: parent.width
                    height: settingView.rowHeight
                    visible: autoPage.value
                    name: qsTr("Auto Page Turning Time Out")
                    value: engine.setting(REngine.AutoPageTurningTimeOut)
                    onClicked: {
                        valueSelectionDialog.titleStr = autoPageTimeOut.name
                        valueSelectionDialog.minimumValue = 5
                        valueSelectionDialog.maximumValue = 300
                        valueSelectionDialog.value = autoPageTimeOut.value
                        valueSelectionDialog.selectItem = autoPageTimeOut
                        valueSelectionDialog.open()
                    }
                }
                SettingViewBoolRow {
                    id: showAutoPageTime
                    textColor: settingView.fontColor
                    width: parent.width
                    height: settingView.rowHeight
                    visible: autoPage.value
                    name: qsTr("Show Auto Page Turning Time")
                    value: engine.setting(REngine.ShowAutoPageTurningTime)
                    onValueChanged: {
                        console.log("showAutoPageTime value changed:",showAutoPageTime.value)
                        if (showAutoPageTime.value === true) {
                            console.log("Show info banner")
                            infoBanner.text = qsTr("Please click timer progress bar to Pause/Resume auto page turning")
                            infoBanner.show()
                        }

                        engine.setSetting(REngine.ShowAutoPageTurningTime, value)
                    }
                }
                SettingViewBoolRow {
                    id: showToolBar
                    textColor: settingView.fontColor
                    width: parent.width
                    height: settingView.rowHeight
                    name: qsTr("Show Toolbar")
//                    enabled: showQuickSetting.value
                    value: engine.setting(REngine.ShowToolbar)
                    onValueChanged: {
                        engine.setSetting(REngine.ShowToolbar, value)
                    }
                }
                SettingViewTextRow {
                    id: orientation
                    textColor: settingView.fontColor
                    width: parent.width
                    height: settingView.rowHeight
                    name: qsTr("Orientation")
                    value: orientationString(engine.setting(REngine.Orientation))
                    onClicked: {
                        orientationMenu.open()
                    }
                }
                SettingViewBoolRow {
                    id: hideEmptyLine
                    textColor: settingView.fontColor
                    width: parent.width
                    height: settingView.rowHeight
                    name: qsTr("Hide Empty Line")
                    value: engine.setting(REngine.TextIgnoreEmptyLine)
                    onValueChanged: {
                        engine.setSetting(REngine.TextIgnoreEmptyLine, value)
                    }
                }
                SettingViewBoolRow {
                    id: showQuickSetting
                    textColor: settingView.fontColor
                    width: parent.width
                    height: settingView.rowHeight
                    name: qsTr("Show Quick Setting")
                    value: engine.setting(REngine.ShowQuickSetting)
                    onValueChanged: {
                        engine.setSetting(REngine.ShowQuickSetting, value)
                        if (value == true) {
                            showToolBar.enabled = true
                        }
                        else {
                            if (!showToolBar.value) {
                                console.log("Show info banner")
                                infoBanner.text = qsTr("Force to show toolbar")
                                infoBanner.show()
                            }

                            showToolBar.enabled = false
                            showToolBar.value = true
                        }
                    }
                }
                SettingViewTextRow {
                    id: fileCodec
                    visible: currentFile.length > 0
                    textColor: settingView.fontColor
                    width: parent.width
                    height: settingView.rowHeight
                    name: qsTr("Current File Codec")
                    value: engine.fileCurrentCodec(currentFile)
                    onClicked: {
//                        codecSelectionDialog.selectItem = fileCodec
//                        codecSelectionDialog.supportAuto = true
//                        codecSelectionDialog.reset()
//                        codecSelectionDialog.open()
                        settingView.pageStack.push(Qt.resolvedUrl("CodecSelectionView.qml"),
                                                   {fileName: settingView.currentFile,
                                                       selectForFile: true,
                                                       addAuto:false,
                                                       addMore:true})
                    }
                }
                SettingViewTextRow {
                    id: defaultCodec
                    textColor: settingView.fontColor
                    width: parent.width
                    height: settingView.rowHeight
                    name: qsTr("Default File Codec")
                    value: engine.setting(REngine.Codec) === "Auto" ? qsTr("Auto") : engine.setting(REngine.Codec)
                    onClicked: {
//                        codecSelectionDialog.selectItem = defaultCodec
//                        codecSelectionDialog.supportAuto = false
//                        codecSelectionDialog.reset()
//                        codecSelectionDialog.open()
                        settingView.pageStack.push(Qt.resolvedUrl("CodecSelectionView.qml"),
                                                   {fileName: "",
                                                       selectForFile: false,
                                                       addAuto:true,
                                                       addMore:true})
                    }
                }
            }
        }
    }



//    CodecSelectionDialog{
//        id: codecSelectionDialog
//        property Item selectItem: null
//        onAccepted: {
//            if (selectItem == defaultCodec) {
//                engine.setSetting(REngine.Codec,
//                                  codecSelectionDialog.model.get(
//                                      codecSelectionDialog.selectedIndex).name)
//            }
//            else if (selectItem == fileCodec) {
//                engine.saveFileCodec(currentFile,
//                                     codecSelectionDialog.model.get(
//                                         codecSelectionDialog.selectedIndex).name)
//            }

//            reload()
//        }
//    }

    RValueSelectionDialog {
        id: valueSelectionDialog
        property Item selectItem: null
        onAccepted: {
            console.log("valueSelectDialog accepted, value", valueSelectionDialog.value)
            if (value < minimumValue) {
                value = minimumValue
            }
            else if (value > maximumValue) {
                value = maximumValue
            }

            if (valueSelectionDialog.selectItem == lineSpace) {
                engine.setSetting(REngine.TextLineSpace, valueSelectionDialog.value)
            }
            else if (valueSelectionDialog.selectItem == wordSpace) {
                engine.setSetting(REngine.TextFontWordSpacing, valueSelectionDialog.value)
            }
            else if (valueSelectionDialog.selectItem == fontSize) {
                engine.setSetting(REngine.TextFontPixelSize, valueSelectionDialog.value)
            }
            else if (valueSelectionDialog.selectItem == letterSpace) {
                engine.setSetting(REngine.TextFontLetterSpacing, valueSelectionDialog.value)
            }
            else if (valueSelectionDialog.selectItem == autoPageTimeOut) {
                engine.setSetting(REngine.AutoPageTurningTimeOut, valueSelectionDialog.value)
            }

            reload()
        }
    }

    ColorSelectionDialog {
        id: colorSelectionDialog
        property Item selectItem: null
        property color selectedColor
        onAccepted: {
            console.log("colorSelectionDialog accepted, color", colorSelectionDialog.model.get(selectedIndex).name)
            selectedColor = colorSelectionDialog.model.get(selectedIndex).name
            if (selectItem == backgroundColor) {
                engine.setSetting(REngine.TextBackgroud, colorSelectionDialog.selectedColor)
            }
            else if (selectItem == textColor) {
                engine.setSetting(REngine.TextColor, colorSelectionDialog.selectedColor)
            }
            reload()
        }
    }

    FontSelectionDialog {
        id: fontSelectionDialog
        onAccepted: {
            console.log("fontSelectionDialog accepted, font", fontSelectionDialog.model.get(selectedIndex).name)
            var selectedFont = fontSelectionDialog.model.get(selectedIndex).name
            engine.setSetting(REngine.TextFontFamiliy, selectedFont)
            reload()
        }
    }

    function saveOrientation(orientation) {
        screen.allowedOrientations = orientation
        engine.setSetting(REngine.Orientation, screen.allowedOrientations)
    }

    ContextMenu {
        id: orientationMenu
        MenuLayout {
            id: orientationMenuLayout
            MenuItem {
                text: qsTr("Auto")
                onClicked: {
                    saveOrientation(Screen.All)
                    reload()
                }
            }
            MenuItem {
                text: qsTr("Portrait")
                onClicked: {
                    saveOrientation(Screen.Portrait)
                    reload()
                }
            }
            MenuItem {
                text: qsTr("Landscape")
                onClicked: {
                    saveOrientation(Screen.Landscape)
                    reload()
                }
            }
            MenuItem {
                text: qsTr("Portrait Inverted")
                onClicked: {
                    saveOrientation(Screen.PortraitInverted)
                    reload()
                }
            }
            MenuItem {
                text: qsTr("Landscape Inverted")
                onClicked: {
                    saveOrientation(Screen.LandscapeInverted)
                    reload()
                }
            }
            MenuItem {
                text: qsTr("Current")
                onClicked: {
                    saveOrientation(screen.currentOrientation)
                    reload()
                }
            }
        }
    }
    onStatusChanged: {
        console.log("status changed:", status)
        if (status == PageStatus.Activating) {
            console.log("reload setting")
//            COM.disableDisplayBlank(true)
            reload()
        }
        else if (status == PageStatus.Activating) {
            screen.allowedOrientations = engine.setting(REngine.Orientation)
        }
        else if (status == PageStatus.Deactivating) {
//            COM.disableDisplayBlank(false)
        }
    }
    tools: ToolBarLayout {
        ToolIcon {
            id: backTool
            iconId: "toolbar-back"
            onClicked: {
                console.log("Back button clicked")
                settingView.pageStack.pop()
            }
        }
    }

//    Connections {
//        target: platformWindow
//        onViewModeChanged: {
//            console.log("viewModeChanged", platformWindow.viewMode)
//            COM.handleViewModeVisibleChanged()
//        }
//        onVisibleChanged: {
//            console.log("visibleChanged", platformWindow.visible)
//            COM.handleViewModeVisibleChanged()
//        }
//    }
}
